//
//  ReSetPasswordVC.m
//  投融保
//
//  Created by 523 on 14-1-19.
//  Copyright (c) 2014年 木叶科技. All rights reserved.
//

#import "ReSetPasswordVC.h"
#import "MYWebService.h"
#import "MYLoginViewController.h"
@interface ReSetPasswordVC ()

@end

@implementation ReSetPasswordVC
{
    int i;//倒计时间
    NSTimer *timer;
}
extern NSString *oldeBindPhone;
extern NSString *key;
extern NSString *resetPduserName;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.txtPhone.layer setCornerRadius:5.0f];
    self.txtPhone.text=oldeBindPhone;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnGetCaptcha:(UIButton *)sender {
    NSString *strAppMobileService=[NSString stringWithFormat:@"service=appMobileService&mobile=%@&business=ForgetLoginPassWord",self.txtPhone.text];
    
    MYWebService *captchaService=[MYWebService new];
    //菊花
    MBProgressHUD *mbHud = [[MBProgressHUD alloc] initWithView:self.view];
    mbHud.labelText = @"正在发送";
    [self.view addSubview:mbHud];
    mbHud.delegate =self;
    [mbHud showAnimated:YES whileExecutingBlock:^{
        //z菊花转动的时候 开始加载数据
        [captchaService getDataSynWithKey:key encryptedString:strAppMobileService appKey:nil];
    } completionBlock:^{
        //执行加载数据完成后的操作。
        NSDictionary *resulDict=[captchaService.profileDataSyn objectForKey:@"result"];
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:[resulDict valueForKey:@"message"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil  , nil];
        i=60;
        timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countdown:) userInfo:nil repeats:YES];
        [alert show];
    }];
}

- (void)countdown:(NSTimer *)atimer
{
    i--;
    [self.btnSendCaptcha setTitle:[NSString stringWithFormat:@"获取验证码（%d）",i] forState:UIControlStateNormal];
    if (i==0) {
        [timer invalidate];
        [self.btnSendCaptcha setTitle:@"获取验证码（重发）" forState:UIControlStateNormal];
    }
    
    
}

- (IBAction)btnSubmit:(UIButton *)sender {
    [timer invalidate];
    [self.btnSendCaptcha setTitle:@"获取验证码（重发）" forState:UIControlStateNormal];
    NSString *strForgotPasswordService=[NSString stringWithFormat:@"service=forgotPasswordService&newPassword=%@&userName=%@&pwdType=logPassword&verifyCode=%@&business=ForgetLoginPassWord&mobile=%@",self.txtNewPd.text,resetPduserName,self.txtCaptcha.text,oldeBindPhone];
    MYWebService *service=[MYWebService new];
    MBProgressHUD *mbHud = [[MBProgressHUD alloc] initWithView:self.view];
    mbHud.labelText = @"正在加载";
    [self.view addSubview:mbHud];
    mbHud.delegate =self;
    [mbHud showAnimated:YES whileExecutingBlock:^{
        //z菊花转动的时候 开始加载数据
        [service getDataSynWithKey:key encryptedString:strForgotPasswordService appKey:nil];
    } completionBlock:^{
        //执行加载数据完成后的操作。
        NSDictionary *resultDcit=[service.profileDataSyn objectForKey:@"result"];
        if ([[resultDcit valueForKey:@"code"] intValue] ==1) {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:[resultDcit valueForKey:@"message"] delegate:self cancelButtonTitle:@"直接登陆" otherButtonTitles:nil, nil];
            [alert show];
        }else
        {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:[resultDcit valueForKey:@"message"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }];
    
    
    
    
}

- (IBAction)btnReturn:(UIButton *)sender {
//    [self dismissModalViewControllerAnimated:YES];
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)closeKeyboard:(UITextField *)sender {
    [self.bgScrollview setContentOffset:CGPointMake(0, 0) animated:YES];
    [self.view endEditing:YES];
}

#pragma mark +++++++++键盘+++++++++++
int prewTag ;  //编辑上一个UITextField的TAG,
float prewMoveY; //编辑的时候移动的高度
/**
 开始编辑UITextField的方法
 */
-(void) textFieldDidBeginEditing:(UITextField *)textField
{
    
    CGRect textFrame =  textField.frame;
    float textY = textFrame.origin.y+textFrame.size.height;
    float bottomY = self.bgScrollview.frame.size.height-textY;
    if(bottomY>=260)  //判断当前的高度是否已经有216，如果超过了就不需要再移动主界面的View高度
    {
        prewTag = -1;
        return;
    }
    prewTag = textField.tag;
    float moveY;
    if (SCREEN_HEIGHT>480) {
        moveY = 260-bottomY;
    }else
    {
        moveY = 300-bottomY;
    }
    prewMoveY = moveY;
    [UIView animateWithDuration:0.25 animations:^{
        [self.bgScrollview setContentOffset:CGPointMake(0, prewMoveY) animated:YES];
    }];
    
}

/**
 结束编辑UITextField的方法，让原来的界面还原高度
 */
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    
    if (textField==self.txtConfirm) {
        if ((![textField.text isEqualToString:self.txtNewPd.text])&&textField.text.length>0) {
            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"密码输入不一致" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }
    }
    
    if(prewTag == -1) //当编辑的View不是需要移动的View
    {
        return;
    }
    float moveY ;
    
    if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
    {   //还原界面
        moveY =  prewMoveY;
        [self.bgScrollview setContentOffset:CGPointMake(0, moveY)];
    }
    
}

#pragma mark ----------alert代理----------
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            //            [self presentModalViewController:[MYLoginViewController new] animated:YES];
            [self presentViewController:[MYLoginViewController new] animated:YES completion:nil];
            break;
            
        default:
            break;
    }
}

#pragma mark-   -----------状态栏底色------
- (UIStatusBarStyle)preferredStatusBarStyle
{
    [self.view setBackgroundColor:[UIColor blackColor]];
    return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
    return NO;
}
@end
